package com.boco.eoms.report.statistic.util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * created at 2007-09-29
 * provide the method to connect to the database
 * @author lzp
 *
 */
public class Conn {
	private Connection connection=null;
	private static Log logger=LogFactory.getLog(Conn.class);
	public Connection getConnection(){
		try{
			if((connection==null)||(!connection.isClosed())){
				Properties properties=new Properties();
				try{
					properties.load(getClass().getResourceAsStream("/config/database.properties"));
					Class.forName(properties.getProperty("db.driver"));
					connection=DriverManager.getConnection(properties.getProperty("db.url"),
							properties.getProperty("db.username"),properties.getProperty("db.password"));
				}catch(SQLException e){
					System.out.println("SQLException found!");
					e.printStackTrace();
				}catch(ClassNotFoundException ex){
					System.out.println("Class Not Found !");
					ex.printStackTrace();
				}catch(IOException exp){
					System.out.println("File Not Found !");
					exp.printStackTrace();
				}				
			}
		}catch(SQLException e){
			System.out.println("Connection can't get!");
			e.printStackTrace();
			return null;
		}
		return connection;
	}
	public Connection getConnection(String driver,String url,String username,String password){
		try{
			if((connection==null)||(!connection.isClosed())){
				try{
					Class.forName(driver);
					connection=DriverManager.getConnection(url, username, password);
				}catch(SQLException e){
					System.out.println("Can't use the provided message create a connection");
					System.err.println(e);
				}catch(ClassNotFoundException e){
					System.out.println("Can't find the Driver Class");
					System.err.println(e);
				}
			}
		}catch(SQLException e){
			System.out.println("Connection can't get!");
			System.err.println(e);
			return null;
		}
		return connection;
	}
	/*****  关闭数据库连接   */
	public static void close(Connection connection){
		if(connection!=null){
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				System.out.println(" close con:" + e.getMessage());
			}
		}
		connection=null;
		logger.info("Connection has been closed!");
	}
	/***** 关闭结果集 */
	public static void close(ResultSet rs){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				System.out.println("close rs:"+e.getMessage());
			}
		}
		rs=null;
		logger.info("ResultSet has been closed!");
	}
	
	/***** 关闭PreparedStatement */
	public static void close(PreparedStatement ps){
		if(ps!=null){
			try{
				ps.close();
			}catch(SQLException e){
				System.out.println("close ps:"+e.getMessage());
			}
		}
		ps=null;
		logger.info("PreparedStatement has been closed!");
	}
	/***** 关闭Statement *****/
	public static void close(Statement st){
		if(st!=null){
			try{
				st.close();
			}catch(SQLException e){
              //TODO Auto-generated catch block
				System.out.println("close st:"+e.getMessage());
			}
		}
		st=null;
		logger.info("Statement has been closed!");
	}

}
